"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/4 3:47 下午
@file: gaolu_score_model.py
@Desc: GaoLuScoreModel
"""

from flask import current_app

from app.models.bo.gaolu.gaolu_model_score_bo import GaoLuModelScoreBO
from app.models.bo.gaolu.gaolu_model_input_bo import GaoLuModelInputBO
from app.services.models.pickle_model import PickleModel


class GaoLuScoreModel(PickleModel):
    # 指标整体权重表

    gaolu_model_input_bo: GaoLuModelInputBO = None

    def __init__(self):
        super().__init__(model_name="gaolu_score_model")

    def process(self):
        gaolu_model_input_bo: GaoLuModelInputBO = self.gaolu_model_input_bo
        try:
            LFLL = gaolu_model_input_bo.CG_LT_GL_GL04_LFLL

            if LFLL is None:
                LFLL = 0
            warning = None
            if LFLL <= 4000:
                warning = "炉况失常，休风"
                score = 3 * LFLL / 200
            else:
                score = self.get_score(self.model_instance,
                                       gaolu_model_input_bo.get_value_dict())
            status = self._get_status_from_score(score)

            if 4000 < LFLL <= 5200:
                warning = "炉况异常，慢风"
            if 5200 <= LFLL:
                warning = "炉况正常，全风"

            score = score * 1.05

            return GaoLuModelScoreBO(score=score,
                                     status=status, status_warning=warning)
        except Exception as e:
            current_app.logger.error(e, exc_info=1)
            return GaoLuModelScoreBO(score=0,
                                     status="数据异常", status_warning=None)

    # 获取子分数函数
    def get_sub_score(self, score_dict, param, value):
        ld = score_dict.get(param,None)

        if ld is None:
            return 0

        for interval in ld.keys():
            if value in interval:
                return ld[interval]
        return 60.0

    # 获取整体分数函数
    def get_score(self, score_dict, value_dict):
        score = 0
        score1 = 0
        score2 = 0
        score3 = 0
        for k in self.score1_weight.keys():
            score1 += self.get_sub_score(score_dict, k, value_dict[k]) * self.score1_weight[k]

        for k in self.score2_weight.keys():
            score2 += self.get_sub_score(score_dict, k, value_dict[k]) * self.score2_weight[k]

        for k in self.score3_weight.keys():
            score3 += self.get_sub_score(score_dict, k, value_dict[k]) * self.score3_weight[k]

        score = (score1 * 0.45 + score2 * 0.2 + score3 * 0.35) * 1.05
        return score

    def _get_status_from_score(self, score):
        if score > 80:
            return '优秀'
        elif 70 <= score < 80:
            return '良好'
        elif 60 <= score < 70:
            return '及格'
        elif score < 60:
            return '不及格'
        else:
            return '数据异常'

    score1_weight = dict(CG_LT_GL_GL04_FCokeRate=0.15,
                         CG_LT_GL_GL04_FCoalRate=0.10,
                         CG_LT_GL_GL04_RFWD=0.05,
                         CG_LT_GL_GL04_FYLL=0.2,
                         CG_LT_GL_GL04_LFLL=0.3,
                         CG_LT_GL_GL04_GFDNKG=0.1,
                         CG_LT_GL_GL04_RFH=0.1)

    score2_weight = dict(CG_LT_GL_GL04_DW=0.1,
               CG_LT_GL_GL04_LDYL=0.05,
               CG_LT_GL_GL04_YC=0.1,
               CG_LT_GL_GL04_TQXZS=0.25,
               CG_LT_GL_GL04_MQLYL=0.15,
               CG_LT_GL_GL04_ZXQL=0.05,
               CG_LT_GL_GL04_XBYCZB=0.05,
               CG_LT_GL_GL04_Ftswd=0.1,
               CG_LT_GL_GL04_19507WDPJ=0.05,
               CG_LT_GL_GL04_26275WDPJ=0.05,
               CG_LT_GL_GL04_34800WDPJ=0.05)

    score3_weight = dict(
               CG_LT_GL_GL04_V=0.05,
               CG_LT_GL_GL04_Si_Ti=0.05,
               CG_LT_GL_GL04_FCoalRate=0.2,
               CG_LT_GL_GL04_FCokeRate=0.25,
               CG_LT_GL_GL04_FLiLunTie=0.45)


# 用导出module的方法创建单例
gaolu_score_model = GaoLuScoreModel()
